package com.vict.bean.appversion.ao;

import com.alibaba.fastjson.annotation.JSONField;
import com.vict.entity.AppVersion;
import com.vict.enums.app.Platform;
import com.vict.framework.fastjsonserializer.EnumDeserializer;
import com.vict.framework.fastjsonserializer.EnumSerializer;
import com.vict.framework.fastjsonserializer.LongDeserializer;
import com.vict.framework.fastjsonserializer.LongSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class UpdateVersionAO {

    @ApiModelProperty("id")
    @JSONField(serializeUsing = LongSerializer.class, deserializeUsing = LongDeserializer.class)
    private Long id;

    @ApiModelProperty("平台 Android IOS")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private Platform platform;

    @ApiModelProperty("更新内容,功能介绍")
    private String info;

    @ApiModelProperty("更新包")
    private String fileUrl;

    @ApiModelProperty("wgt更新包")
    private String wgtUrl;

    @ApiModelProperty("更新方式 APK WGT")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private AppVersion.UpdateMethod updateMethod;

    @ApiModelProperty("是否强制 yes no")
    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private AppVersion.IsForce isForce;

    public void check() {
        if (id == null) {
            throw new RuntimeException("id不能为空");
        }

        if (info == null) {
            throw new RuntimeException("更新内容不能为空");
        }

        if ((fileUrl == null || fileUrl.trim().equals("")) && (wgtUrl == null || wgtUrl.trim().equals(""))) {
            throw new RuntimeException("更新包不能为空");
        }

        if(wgtUrl != null && !wgtUrl.trim().equals("") && (fileUrl == null || fileUrl.trim().equals(""))){
            throw new RuntimeException("上传热更包必须上传全量包");
        }

        if(updateMethod == null){
            throw new RuntimeException("更新方式不能为空");
        }

        if(isForce == null){
            throw new RuntimeException("是否强制不能为空");
        }

        if(updateMethod == AppVersion.UpdateMethod.WGT){
            if(wgtUrl == null || wgtUrl.trim().equals("")){
                throw new RuntimeException("请上传wgt更新包");
            }
        }else if(updateMethod == AppVersion.UpdateMethod.APK){
            if(fileUrl == null || fileUrl.trim().equals("")){
                throw new RuntimeException("请上传apk更新包");
            }
        }

        if(platform == Platform.Android){
            if(updateMethod == AppVersion.UpdateMethod.WGT){
                if(wgtUrl == null || wgtUrl.trim().equals("")){
                    throw new RuntimeException("请上传wgt更新包");
                }
            }else if(updateMethod == AppVersion.UpdateMethod.APK){
                if(fileUrl == null || fileUrl.trim().equals("")){
                    throw new RuntimeException("请上传apk更新包");
                }
            }
        }else if(platform == Platform.IOS){
            if(updateMethod == AppVersion.UpdateMethod.WGT){
                if(wgtUrl == null || wgtUrl.trim().equals("")){
                    throw new RuntimeException("请上传wgt更新包");
                }
            }else if(updateMethod == AppVersion.UpdateMethod.IPA){
                if(fileUrl == null || fileUrl.trim().equals("")){
                    throw new RuntimeException("请上传ipa更新包");
                }
            }
        }
    }
}
